package Lee_code;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Stack;

/**
 * @author blh
 * @date 2024年10月23日 14:13
 */
public class Lee_503_1023 {
    public int[] nextGreaterElements(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        int max = Arrays.stream(nums).max().getAsInt();

        for (int i = 0; i < res.length; i++) {
            if (nums[i] == max) {
                res[i] = -1;
            }
        }
        Stack<Integer> deque = new Stack<>();
        deque.push(0);
        for (int i = 1; i < len * 2 - 1; i++) {
            int realIndex = i % len;
            while (!deque.isEmpty() && nums[deque.peek()] < nums[realIndex]) {
                res[deque.pop()] = nums[realIndex];
            }
            deque.push(realIndex);
        }
        return res;
    }

    public static void main(String[] args) {
        Lee_503_1023 method = new Lee_503_1023();
        method.nextGreaterElements(new int[]{1, 2, 1});
    }
}
